﻿using HalconDotNet;
using Kimd.Vision;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Kimd.Motion
{
    [Serializable]
    public class Tray
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        public string XAxisName { get; set; }
        public string YAxisName { get; set; }
        public string ZAxisName { get; set; }
        public string RAxisName { get; set; }
        private int _rowCount;
        public int RowCount
        {
            get { return _rowCount; }
            set { _rowCount = value; }
        }
        private int _colCount;
        public int ColCount
        {
            get { return _colCount; }
            set { _colCount = value; }
        }
        private TrayDir _direction;
        public TrayDir Direction
        {
            get { return _direction; }
            set { _direction = value; }
        }
        private int _getIndex;
        public int GetIndex
        {
            get { return _getIndex; }
            set { _getIndex = value; }
        }
        public PointF LeftUpPoint { get; set; }
        public bool IsLeftUpSet { get; set; }
        public PointF RightUpPoint { get; set; }
        public bool IsRightUpSet { get; set; }
        public PointF LeftDownPoint { get; set; }
        public bool IsLeftDownSet { get; set; }
        public PointF RightDownPoint { get; set; }
        public bool IsRightDownSet { get; set; }
        public int NozzleCount { get; set; }
        public double OffsetX { get; set; }
        public double OffsetY { get; set; }
        public double NozzleZ { get; set; }
        public double NozzleR { get; set; }

        //产品相关
        [XmlIgnore]
        public string Barcode { get; set; }
        [XmlIgnore]
        public TrayHelper Helper { get; set; }
        //ui相关
        [XmlIgnore]
        public int CellWidth { get; set; }
        [XmlIgnore]
        public int CellHeight { get; set; }
        [XmlIgnore]
        public int CellOffsetX { get; set; }
        [XmlIgnore]
        public int CellOffsetY { get; set; }
        [XmlIgnore]
        public List<Rectangle1NB> CellShapes { get; set; }
        public Tray()
        {
            Helper = new TrayHelper(this);
        }
        public Tray(int row, int col)
        {
            this.RowCount = row;
            this.ColCount = col;
            Helper = new TrayHelper(this);
        }
        public Tray(string xName, string yName, string zName, string rName, 
                    int rowCount, int colCount, TrayDir direction, 
                    int nozzleCount = 1, double offsetX = 0, double offsetY = 0, double nozzleZ = 0, double nozzleR = 0)
        {
            this.XAxisName = xName;
            this.YAxisName = yName;
            this.ZAxisName = zName;
            this.RAxisName = rName;
            this.NozzleCount = nozzleCount;
            this.OffsetX = offsetX;
            this.OffsetY = offsetY;
            this.NozzleZ = nozzleZ;
            this.NozzleR = nozzleR;
            Helper = new TrayHelper(this);
        }
        public void CreateRectangles(bool isReverse = false)
        {
            CellShapes = new List<Rectangle1NB>();
            for (int i = 0; i < RowCount; i++)
            {
                for (int j = 0; j < ColCount; j++)
                {
                    Rectangle1NB rect = new Rectangle1NB()
                    {
                        StartPoint = new PointF(CellWidth * j + CellOffsetX * (j + 1), CellHeight * i + CellOffsetY * (i + 1)),
                        EndPoint = new PointF(CellWidth * j + CellOffsetX * (j + 1) + CellWidth, CellHeight * i + CellOffsetY * (i + 1) + CellHeight),
                        IsFill = true,
                        IsSelected = false,
                        IsShowOnly = true,
                        Alias =isReverse ? (RowCount * ColCount - 1 - Helper.GetCellIndex(i, j)).ToString() : Helper.GetCellIndex(i, j).ToString(),
                        PaintColor = CellColor.Instance.Status2Color[CellStatus.NoProduct],
                    };
                    CellShapes.Add(rect);
                }
            }
        }
    }
    public enum TrayDir
    {
        [Description("XZ")]
        XZ,
        [Description("YZ")]
        YZ,
        [Description("XS")]
        XS,
        [Description("YS")]
        YS
    }
}
